package com.org.wareon.main;

import java.awt.*;
import java.awt.event.*;

import java.io.*;

import java.util.*;

import javax.swing.*;


public class F extends JFrame implements Runnable, ActionListener {
    private JButton b;
    private JLabel message;

    private void setGui() {
        try {
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	    setLocation(0, 0);
	    Container cp = getContentPane();
	    cp.setLayout(new FlowLayout());
	    b = new JButton("Click me auto!");
	    b.addActionListener(this);
	    cp.add(b);
	    message = new JLabel("      ");
	    cp.add(message);
	    new Thread(this).start();
	} catch (Exception e) {
	    e.printStackTrace();
	}
    }

    public void actionPerformed(ActionEvent e) {
	//JOptionPane.showMessageDialog(this, "That was easy!");
	message.setText("Thank you robot!");
    }

    public void run() {
	try {
		
	    Thread.sleep(1500L);
	    PointerInfo a = MouseInfo.getPointerInfo();
		Point bit  = a.getLocation();
		int xBit = (int)bit.getX();
		int yBit = (int)bit.getY();
		System.out.println(xBit+"  :  "+yBit);
	    Point bxy = b.getLocation();
	    int bw = b.getWidth();
	    int bh = b.getHeight();
	    Point p = new Point(bxy.x + bw/2, bxy.y + bh/2);
	    System.out.printf("Button centre located at (%d,%d)\n in content pane coord space", p.x, p.y);
	    SwingUtilities.convertPointToScreen(p, this.getContentPane());
	    System.out.printf("Need to move to (%d,%d)\n", p.x, p.y);

	    final Robot r = new Robot();
	    int x;
	    int y;
	    x = y = 0;

	   /* while ((x < p.x) || (y < p.y)) {
			r.mouseMove(x, y);
			x = Math.min(p.x, x);
			y = Math.min(p.y, y);
			x++;
			y++;
			Thread.sleep(0);
	    }*/
	    r.mouseMove(p.x, p.y);
	    System.out.printf("Pointer now located at (%d,%d)\n", x, y);
	    r.mousePress(InputEvent.BUTTON1_MASK);
	    r.delay(0);
	    r.mouseRelease(InputEvent.BUTTON1_MASK);
	    r.mouseMove(xBit, yBit);
	    System.out.println((int)bit.getX()+"  :  "+(int)bit.getY());
	} catch (Exception e) {
	    /* ignore */
	}
    }

    public static void main(String[] args) {
	try {
	    SwingUtilities.invokeAndWait(new Runnable() {
		public void run() {
		    F f = new F();
		    f.setGui();
		    f.setSize(200, 200);
		    f.setVisible(true);
		}
	    });
	} catch (Exception e) {
	    e.printStackTrace();
	}
    }
}

